<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .box{
            width: 90%;
            margin: 0 auto;
            overflow: hidden;
            position: relative;
        }
        ul{
            display: flex;
            position: relative;
            left: 0;
            transition: left .2s linear;
        }
        ol{
            display: flex;
            position: absolute;
            bottom: 10px;
            width: 100%;
            left: 0;
            justify-content: center;
        }
        ol > li{
            width: 10px;
            height: 10px;
            background-color: #fff;
            margin: 0 8px;
        }
        ol > li.action{
            background-color: black;
        }
        img{
            border-radius: 5px;
            display: block;
        }
        .boximg{
            width: 90vw;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul>
            <!-- <li>
                <img src="" alt="">
            </li> -->
        </ul>
        <ol>
            <!-- <li></li> -->
        </ol>
    </div>
    <script>
        var image = ["https://m15.360buyimg.com/mobilecms/s1062x420_jfs/t1/33428/39/16450/106681/6311cad7Ef9b3d856/f10720b7f8f38937.jpg!cr_1053x420_4_0!q70.jpg",
        "https://m15.360buyimg.com/mobilecms/jfs/t1/3230/40/20384/44107/62daf2d9E5c5e0e55/03ab360c40ddc266.jpg!cr_1125x449_0_166!q70.jpg",
        "https://m15.360buyimg.com/mobilecms/jfs/t1/110775/22/28940/94917/62e5bc6dE569b2244/5d14fc9f06d87341.jpg!cr_1125x449_0_166!q70.jpg"];
        
        var ul = document.querySelector(".box > ul");
        var ol = document.querySelector(".box > ol");
        var box = document.querySelector(".box");

        var str = "",str1 = "";
        for(var k = 0;k < image.length;k++){
            // console.log(image[k]);
            str += `<li><img class="boximg" src="${image[k]}" alt=""></li>`;
            if(k == 0){
                str1 += `<li class="action"></li>`;
            }else{
                str1 += `<li></li>`;
            }
            
            // console.log("---",str);
        }
        ul.innerHTML = str;
        ol.innerHTML = str1;
        // 给标签设置初始样式
        ul.style.width = image.length * box.offsetWidth + "px";

        // 页面加载完成后默认显示第 0 张
        // 记录当前显示图片(li)的索引值 (点击下一张或者上一张都是由当前显示的图进行上一张或下一张)
        var i = 0;
        // ul.style.left = i * box.offsetWidth + "px";

        var ollis = document.querySelectorAll(".box > ol > li")

        // 点击下一张实现ul的left移动到下一张li的位置
        // 在box上 按下 抬起  记录按下时与抬起时手指触碰的位置
        // 根据两个x判断手指是向左滑还是向右滑了
        var start = 0;
        box.ontouchstart = function(e){
            // console.log("手指按下事件");
            // console.log(e.touches[0].clientX); 
            start = parseInt(e.touches[0].clientX);
        }
        box.ontouchend = function(e){
            // console.log("手指抬起事件");
            // console.log(e.changedTouches[0].clientX); 
            var end = parseInt(e.changedTouches[0].clientX);
            // console.log(start);
            // console.log(end);
            if (start > end && start - end >= 50) {
                // 向左滑动
                // console.log("向左滑动大于10px");
                // 显示下一张
                i++;
                if(i == image.length) i = image.length - 1;
            }else if (end - start >= 50){
                // 向右滑动
                // console.log("向右滑动大于10");
                // 显示上一张
                i--;
                if(i < 0) i = 0;
            }
            var l = i * -box.offsetWidth;
            // console.log(i);
            // console.log(l);
            ul.style.left = l + "px";

            // 小圆点
            for(var m = 0;m < ollis.length;m++){
                ollis[m].classList.remove("action");
            }
            ollis[i].classList.add("action");

        }
    </script>
</body>
</html>